package com.qc.controller.web;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.qc.common.ImageUtils;
import com.qc.common.QiNiuUtil;
import com.qc.entity.BusinessCategoryEntity;
import com.qc.pageUtil.page.page.Page;
import com.qc.pageUtil.page.page.PageModel;
import com.qc.pageUtil.page.page.PageModelList;
import com.qc.pageUtil.page.vo.NewsAndNav;
import com.qc.service.BusinessCategoryService;

import net.sf.json.JSONObject;

@SuppressWarnings({ "rawtypes", "unchecked" })
@Controller
@RequestMapping("/web")
public class WebBusinessCategoryController {

	@Resource(name="businessCategoryService")
	private BusinessCategoryService businessCategoryService;
	
	/**
     * 跳转所有秘书一级到首页
     * @return
     */
	
	@RequestMapping(value = "/toBusinessCategoryIndex", produces = "text/html;charset=UTF-8")
    public ModelAndView toBusinessCategoryIndex() {
        ModelAndView mav = new ModelAndView("/category/leibie_shangjia");
        return mav;
    }
    
    /**
     * 所有秘书一级数据展示
     * @param request
     * @return
     */
    @RequestMapping(value = "/toListAllBusinessCategory", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllBusinessCategory(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = businessCategoryService.getListBusinessCategoryCount(name);
        
        //根据条件查询当前页数据
        List<BusinessCategoryEntity> list = businessCategoryService.getAllBusinessCategory(name, pageNo, pageSize);
        
        //分页查询公共类
        Page<BusinessCategoryEntity> page = new Page<BusinessCategoryEntity>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<BusinessCategoryEntity> pageModel = new PageModelList<BusinessCategoryEntity>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav  objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
    
    /**
     * 所有秘书一级添加
     */
    @RequestMapping(value = "/toSaveBusinessCategory")
    @ResponseBody
    public int toSaveBusinessCategory(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        
        BusinessCategoryEntity businessCategory = new BusinessCategoryEntity();
        
        businessCategory.setName(name);
        businessCategory.setParent("");
        businessCategory.setLevel("1");
        businessCategory.setImgUrl("");
        
        int flag = businessCategoryService.saveBusinessCategory(businessCategory);
        
        return flag;
    }
    
    /**
     * 所有秘书一级删除
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/toDeleteBusinessCategoryId")
    @ResponseBody
    public int toDeleteBusinessCategoryId(HttpServletRequest request,HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        int flag = businessCategoryService.deleteBusinessCategoryId(id);
        return flag;
    }
    
    /**
     * 所有秘书一级修改
     */
    
    @RequestMapping(value = "/toUpdateBusinessCategoryId")
    @ResponseBody
    public int toUpdateBusinessCategoryId(HttpServletRequest request, HttpServletResponse response) {
        long id = Long.parseLong(request.getParameter("id"));
        String name = request.getParameter("name");
        
        
        BusinessCategoryEntity businessCategory = new BusinessCategoryEntity();
        
        businessCategory.setName(name);
        businessCategory.setbId(id);
        
        int flag = businessCategoryService.updateBusinessCategoryId(businessCategory);
        return flag;
    }
    
    /**
     * 跳转所有秘书二级到首页
     * @return
     */
    @RequestMapping(value = "/toBusinessCategoryIndexTwo", produces = "text/html;charset=UTF-8")
    public ModelAndView toBusinessCategoryIndexTwo(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("/category/leibie_shangjia2");
        
        String parent = request.getParameter("parent");

        mav.addObject("parent",parent);
        
        return mav;
    }
    
    /**
     * 所有秘书二级添加
     */
    @RequestMapping(value = "/toSaveBusinessCategory2")
    @ResponseBody
    public int toSaveBusinessCategory2(HttpServletRequest request) {
    	
		String name = request.getParameter("name");
	    String parent = request.getParameter("parent");
        
        BusinessCategoryEntity businessCategory = new BusinessCategoryEntity();
        
        businessCategory.setName(name);
        businessCategory.setParent(parent);
        businessCategory.setLevel("2");
        businessCategory.setImgUrl("");
        
        int flag = businessCategoryService.saveBusinessCategory(businessCategory);
        
        return flag;
    }
    
    /**
     * 所有秘书二级数据展示
     * @param request
     * @return
     */
    @RequestMapping(value = "/toListAllBusinessCategoryTwo", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String toListAllBusinessCategoryTwo(HttpServletRequest request) {
        
        String name = request.getParameter("name");
        
        //上一级属性
        String parent = request.getParameter("parent");
        
        //当前页面
        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        //每页显示多少条
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));
        
        //根据条件总数
        int recordCount = businessCategoryService.getListBusinessCategoryCountTwo(name, parent);
        
        //根据条件查询当前页数据
        List<BusinessCategoryEntity> list = businessCategoryService.getAllBusinessCategoryTwo(name, parent, pageNo, pageSize);
        
        //分页查询公共类
        Page<BusinessCategoryEntity> page = new Page<BusinessCategoryEntity>(pageNo, recordCount,pageSize);
        page.setList(list);//设置结果集  
        PageModelList<BusinessCategoryEntity> pageModel = new PageModelList<BusinessCategoryEntity>(page, "#", PageModel.NUM_MODEL,"doPage","1");//设置页码显示类型  
        //4 组装带翻页导航功能的对象
        NewsAndNav  objs = new NewsAndNav(list, pageModel.getPageModel());
        
        return JSONObject.fromObject(objs).toString();
        
    }
    
    @RequestMapping(value = "/toUpdateBusinessCategoryImage")
    public ModelAndView toUpdateBusinessCategoryImage(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request) throws Exception {
    	
    	ModelAndView mav = new ModelAndView("redirect:/web/toBusinessCategoryIndex.do");
    	
    	long id = Long.parseLong(request.getParameter("id"));
        String url = request.getSession().getServletContext().getRealPath("/")+("/fileImage");
        
        BusinessCategoryEntity businessCategory = businessCategoryService.getBusinessCategoryEntityId(id);
        
        //获取图片名称
        String fileName = file.getOriginalFilename();
        
        //目录不存在 新建一个
        File f1=new File(url); 
        if(!f1.isDirectory()) {
            f1.mkdir();
        }
        //创建时间参数
        SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyyMMddhhmmssSSS");// 文件重命名
        String namefile = bartDateFormat.format(new Date());
        
        InputStream stream = file.getInputStream();// 把文件读入
        
        //图片名字
        fileName = namefile + fileName.substring(fileName.indexOf("."));// 保存文件路径和文件名
        OutputStream bos = new FileOutputStream(url + "/" + fileName);// 建立一个上传文件的路径
        int bytesRead = 0;
        byte[] buffer = new byte[10240];
        while ((bytesRead = stream.read(buffer, 0, 10240)) != -1) {
            // 将文件写入服务器
            bos.write(buffer, 0, bytesRead);
        }
        bos.close();
        stream.close();
        
        String path = url + "/" + fileName;
        
        ImageUtils.scale(path, path, 1, true);
        
        businessCategory.setImgUrl("web_dev_14_qiniu_"+fileName);
        
        try {
            QiNiuUtil.upload(path, "web_dev_14_qiniu_"+fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        businessCategoryService.updateBusinessCategoryId(businessCategory);
        
        return mav;
        		
    }
}
